package com.zzusedating.androidapp;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zzusedating.androidapp.adapters.RecommendRecAdapter;
import com.zzusedating.androidapp.customlayout.TitleBarLayout;
import com.zzusedating.androidapp.model.RecommendItem;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class FollowFansListActivity extends BaseActivity {
    public static final int fans = 0;
    public static final int follows = 1;

    private ArrayList<RecommendItem> items;
    private RecyclerView recyclerView;

    private Callback callback;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_follow_fans_list);

        int which = getIntent().getIntExtra("which", fans);
        TitleBarLayout title = findViewById(R.id.title_follow_fans);

        items = new ArrayList<>();
        recyclerView = findViewById(R.id.rcv_follow_fans);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(new RecommendRecAdapter(items, this));

        init();
        if (which == fans){
            title.setTitle("我的粉丝");
            MyApplication.getClient().newCall(
                    new Request.Builder()
                    .get()
                    .url(String.format(Locale.CHINA, ConstantsTools.GET_FANS_LIST, 20))
                    .build()
            ).enqueue(callback);
        }
        else {
            title.setTitle("我关注的");
            MyApplication.getClient().newCall(
                    new Request.Builder()
                            .get()
                            .url(String.format(Locale.CHINA, ConstantsTools.GET_FOLLOW_LIST, 20))
                            .build()
            ).enqueue(callback);
        }

    }
    private void init(){
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what){
                    case -1:
                        MyApplication.Toast("未登录");
                        break;
                    case 1:
                        Objects.requireNonNull(recyclerView.getAdapter()).notifyDataSetChanged();
                        break;
                    case ConstantsTools.NETWORK_ERROR_I:
                        MyApplication.Toast(ConstantsTools.NETWORK_ERROR_);
                        break;
                }
                return true;
            }
        });
        callback = new BasicCallback(handler) {
            @Override
            public void onResponse(Call call, @NonNull Response response) {
                JSONArray array = (JSONArray) getObject(response);
                if (array != null){
                    List<RecommendItem> temp = new Gson().fromJson(
                            array.toString(), new TypeToken<List<RecommendItem>>(){}.getType());
                    items.clear();
                    items.addAll(temp);
                    handler.sendMessage(ConstantsTools.createMessage(1));
                }
                else {
                    handler.sendMessage(ConstantsTools.createMessage(getCode()));
                }
            }
        };
    }
}
